package com.rlyy.rsj.dao;

import com.rlyy.aibase.db.Dbink;
import com.rlyy.aibase.db.QueryRunner;
import com.rlyy.rsj.db.PostgreExecutor;
import com.rlyy.rsj.db.PostgresqlConn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

public class BaseDao {

    protected static final Logger logger = LoggerFactory.getLogger(BaseDao.class);

    protected static final String selectWithoutWhere = "select %s from %s";

    protected static final String select = "select %s from %s where %s";

    protected static final String insertSql = "insert into %s (%s) values (%s)";

    protected static final String updateSql = "update %s set %s where %s";

    protected static final String insertOrUpdateSql = "insert into %s (%s) values (%s) on conflict (%s) do update set %s";

    public static QueryRunner getQueryRunner(){
        return new QueryRunner();
    }

    /**
     * 获取conn 关闭 conn
     * @param icall
     * @return
     */
    public static Object invokeSql(PostgreExecutor.ICall icall) {
        Connection conn = null;
        try {
            conn = PostgresqlConn.getConnection();
            return icall.invoke(conn);
        } finally {
            Dbink.close(conn);
        }
    }

    public interface ICall {
        Object invoke(Connection conn);
    }


}
